﻿#region File Description
//-----------------------------------------------------------------------------
// SingleAnimationTask
//
// Copyright © 2013 Wave Coorporation. All rights reserved.
// Use is subject to license terms.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using WaveEngine.Framework;
using WaveEngine.Framework.Animation;
#endregion

namespace WaveEngine.Extensions.ITasks
{
    public class SingleAnimationTask : TaskBase
    {
        /// <summary>
        /// The single animation
        /// </summary>
        SingleAnimation singleAnimation;

        /// <summary>
        /// The animation
        /// </summary>
        AnimationUI animation;

        /// <summary>
        /// The dependency property
        /// </summary>
        DependencyProperty dependencyProperty;

        public SingleAnimationTask(Scene scene, string taskName, SingleAnimation singleAnimation, AnimationUI animation, DependencyProperty dependencyProperty)
            : base(scene, taskName)
        {
            this.animation = animation;
            this.singleAnimation = singleAnimation;
            this.dependencyProperty = dependencyProperty;
        }

        /// <summary>
        /// Ons the run.
        /// </summary>
        protected override void performRun()
        {
            this.singleAnimation.Completed += singleAnimation_Completed;
            animation.BeginAnimation(dependencyProperty, singleAnimation);
        }

        /// <summary>
        /// Handles the Completed event of the singleAnimation control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void singleAnimation_Completed(object sender, EventArgs e)
        {
            this.performCompleted();
            this.singleAnimation.Completed -= singleAnimation_Completed;
        }

        /// <summary>
        /// Notifies the cancelled.
        /// </summary>
        protected override void performCancel()
        {
            this.singleAnimation.Completed -= singleAnimation_Completed;
            singleAnimation.CancelCompletedEvent();
            singleAnimation.Stop();
            base.performCancel();
        }

        /// <summary>
        /// Notifies the skip.
        /// </summary>
        /// <returns></returns>
        protected override bool performSkip()
        {
            if (this.IsSkippable)
            {
                this.Cancel();
                return base.performSkip();
            }
            else
                return false;
        }

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return base.ToString() + "-> owner: " + (this.animation.Owner != null ? this.animation.Owner.ToString() : null);
        }

        /// <summary>
        /// Gets the child tasks.
        /// </summary>
        /// <value>
        /// The child tasks.
        /// </value>
        /// <exception cref="System.NotImplementedException"></exception>
        public override IEnumerable<ITask> ChildTasks
        {
            get { return System.Linq.Enumerable.Empty<ITask>(); }
        }
    }
}
